import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.ListIterator;

public class eg8_6 {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<Student>();
        Student stu1 = new Student();
        stu1.setStuName("李雷");
        stu1.setStuAge(21);
        students.add(0, stu1); // 添加到列表开头
        Student stu2 = new Student();
        stu2.setStuName("韩梅梅");
        stu2.setStuAge(22);
        students.add(stu2); // 添加到列表末尾
        Student stu3 = new Student();
        stu3.setStuName("Jack");
        stu3.setStuAge(20);
        students.add(0, stu3); // 添加到列表开头
        students.add(stu1); // 添加到列表末尾
        System.out.println("当前学生人数为：" + students.size() + "，分别为：");
        System.out.println("正序排列");
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            Student stu = iterator.next();
            System.out.println(stu.printStuInfo());
        }
        students.remove(stu1); // 移除第一个匹配的元素
        System.out.println("倒序排列：");
        Collections.reverse(students); // 反转列表
        iterator = students.iterator();
        while (iterator.hasNext()) {
            Student stu = iterator.next();
            System.out.println(stu.printStuInfo());
        }
        Student stu4 = new Student();
        stu4.setStuName("刘鑫");
        stu4.setStuAge(24);
        students.set(1, stu4); // 替换索引为1的元素
        System.out.println("从指定处遍历：");
        ListIterator<Student> listIterator = students.listIterator(1);
        while (listIterator.hasNext()) {
            Student stu = listIterator.next();
            System.out.println(stu.printStuInfo());
        }
    }
}